package com.itheima.wemedia.controller;

import com.itheima.admin.pojo.AdChannel;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.pojo.WmUser;
import com.itheima.wemedia.service.ApiService;
import com.itheima.wemedia.service.WmMaterialService;
import com.itheima.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ApiController {

    @Autowired
    private WmUserService wmUserService;

    @Autowired
    private WmMaterialService wmMaterialService;

    @Autowired
    private ApiService apiService;

    /**
     * 远程调用添加自媒体账号
     * @param wmUser
     * @return
     */
    @PostMapping("/api/wmUser/add")
    public ResultVo<WmUser> add(@RequestBody WmUser wmUser){
        wmUserService.save(wmUser);
        return ResultVo.ok(wmUser);
    }


    /**
     * 通appUserId查询自媒体账号信息
     * @param appUserId
     * @return
     */
    @GetMapping("/api/wmUser/{appUserId}")
    public ResultVo<WmUser> getByAppUserId(@PathVariable(name = "appUserId")Integer appUserId){
        WmUser wmUser = wmUserService.getByAppUserId(appUserId);
        return ResultVo.ok(wmUser);
    }


    /**
     * 添加素材
     * @param wmMaterial
     * @return
     */
    @PostMapping("/api/wmMaterial/add")
    public ResultVo add(@RequestBody WmMaterial wmMaterial){
        if (wmMaterialService.save(wmMaterial)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    /**
     * 获取所有频道
     * @return
     */
    @GetMapping("/channel/channels")
    public ResultVo<List<AdChannel>> channelList(){
        List<AdChannel>channelList = apiService.channelList();
        return ResultVo.ok(channelList);
    }
}
